package com.xinzhi.service.impl;

import com.xinzhi.dao.IUserTestMapper;
import com.xinzhi.entity.UserTest;
import com.xinzhi.service.ITransferService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Mapper
@Service
public class TransferServiceImpl implements ITransferService {
    @Autowired
    private IUserTestMapper ius;
    @Transactional
    @Override
    public String transfer(int id1, int id2, String money) {
        UserTest user1 = ius.getUser(id1);
        UserTest user2 = ius.getUser(id2);

        user1.setUser_money(Integer.parseInt(user1.getUser_money()) - Integer.parseInt(money) + "");
        user1.setUser_money((Integer.parseInt(user2.getUser_money()) + Integer.parseInt(money)) + "");

        int u1 = ius.updateMoneyById(id1, user1.getUser_money());
        int u2 = ius.updateMoneyById(id2, user2.getUser_money());

        return u1 > 0 && u2 > 0 ? "转账成功" : "转账失败";
    }



    @Override
    public int setUserInfo(UserTest ut) {
        return ius.updateUserInfo(ut);
    }

    @Override
    public int setUser(UserTest uts) {
        return ius.updateUser(uts);
    }
}
